﻿using JNPF.Basics.Models;
using JNPF.Data;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 系统设置
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.15 
    /// </summary>
    internal class SysConfigService : DataFactory, SysConfigIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<SysConfigEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                return await db.GetListAsync<SysConfigEntity>(x => !string.IsNullOrEmpty(x.Category));

            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="key">key</param>
        /// <returns></returns>
        public async Task<SysConfigEntity> GetInfo(string key)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<SysConfigEntity>(m => m.Key == key);
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task Save(List<SysConfigEntity> entitys, string category)
        {
            using (var db = this.Basic)
            {

                await db.BeginTransactionAsync();
                await db.DeleteAsync<SysConfigEntity>(x => x.Category.Equals(category));
                await db.InsertAsync(entitys);
                await db.CommitTransactionAsync();
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task Update(List<SysConfigEntity> entitys)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.UpdateAsync(entitys);
                await db.CommitTransactionAsync();
            }
        }
    }
}
